var http = require('http');
var iconv = require('iconv-lite')
var url = require('url');
var log4js = require('log4js');
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('logs/webInterface.log'),
		'webInterface');
var logger = log4js.getLogger('webInterface');

exports.sendRequest = function(url1, url2, callback, errorCallback){
	var res1 = null, res2 = null, error = null;
	try{
		http.get(url1, function(res){
			if(res.statusCode != 200){
				error = 'statusCode is not 200, url: ' + url1 + ', statusCode: ' + res.statusCode;
			}
			var dataBuf = new Buffer('');
			res.on('data', function(data){
				dataBuf = Buffer.concat([dataBuf, data])
	//			if(dataBuf == null){
	//				dataBuf = str;
	//			}else{
	//				dataBuf += str;
	//			}
			})
			res.on('end', function(){
				var str = iconv.decode(dataBuf, 'gbk');
				res1 = str;
				
				if(res2 != null){
					notify(res1, res2);
				}
			})
		})
		
		http.get(url2, function(res){
			if(res.statusCode != 200){
				error = 'statusCode is not 200, url: ' + url2 + ', statusCode: ' + res.statusCode;
			}
			var dataBuf = new Buffer('');
			res.on('data', function(data){
				
				dataBuf = Buffer.concat([dataBuf, data])
	//			if(dataBuf == null){
	//				dataBuf = str;
	//			}else{
	//				dataBuf += str;
	//			}
			})
			res.on('end', function(){
				var str = iconv.decode(dataBuf, 'gbk');
				res2 = str;
				if(res1 != null){
					notify(res1, res2);
				}
			})
		})
	
	}catch(e){
		logger.log(url1 + "     " + url2, e)
	}
	
	function notify(res1, res2){
		if(error != null && (typeof errorCallback != 'undefined')){
			errorCallback(error);
		}else{
			callback(res1, res2)
		}
	}
}